package com.hcrazy.manchester.app.controllers.ticket;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hcrazy.manchester.app.form.CustomerSearchForm;
import com.hcrazy.manchester.app.form.SellerSearchForm;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.services.BookOrderService;
import com.hcrazy.manchester.app.utils.Constant;
import com.hcrazy.manchester.app.utils.Log;

@Controller
@RequestMapping("/admin/ticket")
public class TicketCustomerController {
	
	@Autowired
	private BookOrderService orderService;
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/search_customer",method=RequestMethod.POST)
	public String searchAddress(@ModelAttribute("form") CustomerSearchForm form,
			Model model,HttpServletRequest req){
		
		//重置infoId
		form.setInfoId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllCustomers(form.getPageNum(), form.getSearch());
		model.addAttribute("pageModel", pageModel);
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/ticket/customer";
	}
	
	/**
	 * 跳转至功能信息列表页
	 * @param model
	 */
	@RequestMapping(value="/customer",method=RequestMethod.GET)
	public String seller(Model model,HttpServletRequest req){
		//根据查询参数获取分页对象：默认第一页、分类参数为0、查询字符串为空
		PageModel pageModel = getAllCustomers(1,"");
		model.addAttribute("pageModel", pageModel);
		
		//初始化查询对象
		model.addAttribute("searchForm", new SellerSearchForm());
		
		return "/admin/ticket/customer";
	}
	
	/**
	 * 查询全部信息列表
	 * @param pageNum
	 * @param sortId
	 * @param search
	 * @return PageModel
	 */
	private PageModel getAllCustomers(int pageNum,String search){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);

		try {
			return orderService.getPagedCustomers(model, search, 0,0,Constant.TYPE_BOOK_TICKET);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
}
